package com.rzn.reading.controller;


import com.rzn.reading.JsonResult;
import com.rzn.reading.model.dto.readingProgress.IsReadingLiteratureDTO;
import com.rzn.reading.model.dto.readingProgress.ReadingProgressCreateInputDTO;
import com.rzn.reading.model.dto.readingProgress.UserReadingProgressDTO;
import com.rzn.reading.service.ReadingProgressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 阅读进度表 前端控制器
 * </p>
 *
 * @author rzn
 * @since 2022-02-12
 */
@RestController
@RequestMapping("//readingprogress")
@Api(tags = "阅读进度")
public class ReadingProgressController {
    @Autowired
    private ReadingProgressService readingProgressService;

    @GetMapping("/queryReadingProgress/{userId}/{literatureId}")
    @ApiOperation("查询个人文献阅读进度")
    public JsonResult queryLiteratureProgress(@PathVariable Integer userId, @PathVariable Integer literatureId){
        UserReadingProgressDTO res = readingProgressService.getUserLiteratureProgress(userId, literatureId);
        return JsonResult.success(res);
    }

    @DeleteMapping("/delete/{id}/{state}")
    @ApiOperation("删除个人阅读进度(伪删除")
    public JsonResult deleteLiteratureProgress(@PathVariable Integer id, @PathVariable Integer state){
        Boolean res = readingProgressService.deleteLiteratureProgress(id, state);
        if (res){
            return JsonResult.success();
        }else {
            return JsonResult.error();
        }
    }

    @GetMapping("/queryIsReading/{userId}")
    @ApiOperation("查询个人正在阅读的文献")
    public JsonResult queryIsReadingLiterature(@PathVariable Integer userId){
        List<IsReadingLiteratureDTO> isReadingLiterature = readingProgressService.getIsReadingLiterature(userId);
        return JsonResult.success(isReadingLiterature);
    }

    @PostMapping("/queryReadingProgress")
    @ApiOperation("更新个人文献阅读进度")
    public JsonResult UpdateReadingLiteratureProgress(@RequestBody UserReadingProgressDTO userReadingProgressDTO){
        Boolean res = readingProgressService.updateUserReadingProgress(userReadingProgressDTO);
        if (res){
            return JsonResult.success("更新成功");
        }else {
            return JsonResult.success("更新失败");
        }
    }

    @PostMapping("/insertReadingProgress")
    @ApiOperation("添加个人文献阅读进度（首次阅读）")
    public JsonResult insertLiteratureProgress(@RequestBody ReadingProgressCreateInputDTO readingProgressCreateInputDTO){
        Boolean res = readingProgressService.insertUserReadingProgress(readingProgressCreateInputDTO);
        if (res){
            return JsonResult.success("添加成功");
        }else {
            return JsonResult.success("添加失败");
        }
    }
}

